package com.zpl.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpl.member.entity.Member;
import com.zpl.member.entity.dto.MemberKeyWordDTO;
import com.zpl.member.entity.vo.MemberVO;
import com.zpl.member.mapper.MemberMapper;
import com.zpl.member.service.MemberService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wangyuxin
 * @date 2020/9/28
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;

    @Override
    public boolean insert(Member member) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",member.getPhone());
        List<Member> members = memberMapper.selectList(queryWrapper);
        if (members.size()>0){
            return false;
        }
        memberMapper.insert(member);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        if(memberMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Member member) {
        if (memberMapper.updateById(member) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Member getMember(Long id) {
        return memberMapper.selectById(id);
    }

    @Override
    public List<Member> selectByNickName(String nickName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("nickname", nickName);
        List<Member> memberList =memberMapper.selectList(queryWrapper);
        return memberList;
    }

    @Override
    public Member selectByPhone(String phone) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone",phone);
        Member member=memberMapper.selectOne(queryWrapper);
        if(member!=null){
            return member;
        }
        return null;
    }

    @Override
    public List<Member> getAllMember() {
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Member> memberList=memberMapper.selectList(queryWrapper);
        return memberList;
    }

    @Override
    public IPage<Member> getMembers(MemberKeyWordDTO memberKeyWordDTO) {
        Page page = new Page<>(memberKeyWordDTO.getCurrentPage(), memberKeyWordDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(memberKeyWordDTO.getNickname())){
            queryWrapper.eq("nickname",memberKeyWordDTO.getNickname());
        }
        IPage<Member> iPage = memberMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public IPage<MemberVO> getMemberList(MemberKeyWordDTO memberKeyWordDTO) {
        return null;
    }

    @Override
    public boolean login(String phone, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("password", password);
        List<Member> members = memberMapper.selectList(queryWrapper);
        if (members.size() > 0) {
            return true;
        }
        return false;
    }
}
